import { Observable } from 'rxjs/Rx';
import { Injectable } from '@angular/core';

@Injectable()
export class WebsocketService {

  public websocket:WebSocket;

  constructor() { }

  conncetWebSocket(url:string):Observable<any>{
    this.websocket = new WebSocket(url);
    return new Observable(
      observe=>{
        this.websocket.onmessage = (event)=>observe.next(event.data);
        this.websocket.onerror = (error)=>observe.error(error);
        this.websocket.onclose = ()=>observe.complete();
      }
    )
  }

}
